package com.doxiaopu.students.component.controller;

import com.doxiaopu.students.component.exception.*;
import com.doxiaopu.students.component.result.ResponseResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 当前项目所有控制器类的基类
 */
public class BaseController {
    /**
     * 正确响应时的代号
     */
    public static final Integer SUCCESS=200;
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public ResponseResult<Void> HandleException(Exception e) {
        if(e instanceof DuplicateKeyException) {
            //400-违反了unique的约束
            return new ResponseResult<>(400,e);
        }else if(e instanceof InsertException) {
            //500-插入数据错误
            return new ResponseResult<>(500,e);
        }else if(e instanceof UserNotFoundException){
            //401 用户名找不到
            return  new ResponseResult<>(401,e);
        }else if(e instanceof PasswordNotMathException){
            //402 密码不匹配
            return  new ResponseResult<>(402,e);
        }else if (e instanceof UpdateException){
            //403 更新用户出现异常
            return  new ResponseResult<>(403,e);
        }else if (e instanceof InsufficientBalanceException){
            //601 用户的余额不足，不足以支付产品
            return  new ResponseResult<>(601,e);
        }
        else if (e instanceof NotMerchantException){
            //405 不是商户不能创建团单
            return  new ResponseResult<>(405,e);
        }
        return null;
    }
}
